<?php
/**
 * @package LaravelBoot
 *
 * @internal
 *
 * @author mawenpei
 * @date 2019/4/8 19:51
 * @version
 */
namespace Modules\Service\Logic;

use App\Models\RechargeTask;
use App\Models\User;
use App\Models\RechargeOrder;
use App\Models\RechargeOrderDetail;
use App\Models\UserCoupon;
use Illuminate\Support\Facades\DB;
use App\Models\EverydayReport;
use App\Models\MobileRechargeOrder;
use App\Models\Pay;

class ReportLogic extends BaseLogic
{
    public static function doUpdatePlatform()
    {
        //更新平台信息
        DB::update('update yk_recharge_order a,yk_user b set a.app_platform=b.app_platform,a.app_channel=b.app_channel where a.user_id=b.id and a.app_platform is null');
        DB::update('update yk_pay a,yk_user b set a.app_platform=b.app_platform,a.app_channel=b.app_channel where a.user_id=b.id and a.app_platform is null');
        DB::update('update yk_mobile_recharge_order a,yk_user b set a.app_platform=b.app_platform,a.app_channel=b.app_channel where a.user_id=b.id and a.app_platform is null');
        //更新订单卡信息
        DB::update('update yk_recharge_order a,yk_oil_card b set a.card_type=b.type,a.card_no=b.card_no,a.card_mobile=b.mobile where a.oil_card_id=b.id');
        //更新订单详情卡信息
        DB::update('update yk_recharge_order_detail a,yk_oil_card b set a.card_type=b.type,a.card_no=b.card_no,a.card_mobile=b.mobile where a.oil_card_id=b.id');
        //更新首充时间
        DB::update('update yk_user a,(select user_id,min(paid_at) as pay_at from yk_recharge_order where `status`>0 and `status`<7 group by user_id) as b set a.first_recharge_time=b.pay_at where a.id=b.user_id');
        //更新充值次数
        DB::update('update yk_user a,(select user_id,count(*) as total from yk_recharge_order where `status`>0 and `status`<7 group by user_id) b set a.order_num=b.total where a.id=b.user_id ');
        //更新订单优惠券
        DB::update('update yk_recharge_order a,yk_user_coupon b set a.coupon_amount=b.amount where a.coupon_id=b.id and a.coupon_amount=0');

        //更新订单详情表的状态
        DB::update('update yk_recharge_order_detail set result_status=1 where id in (select recharge_order_detail_id from yk_recharge_task where recharge_order_detail_id>0 and  `status`=9) and result_status=0');
    }

    public static function doAllEveryDay($date)
    {
        self::doEveryDay($date);
        self::doPlatformEveryDay($date,'android','all');
        self::doPlatformEveryDay($date,'ios','all');
        self::doPlatformEveryDay($date,'wechart','all');
        self::doPlatformEveryDay($date,'jrtt','all');

        $other_channels = ['qingming2019','register2019'];
        foreach ($other_channels as $channel){
            self::doIosAndWechartPlatformEveryDay($date,'ios',$channel);
            self::doIosAndWechartPlatformEveryDay($date,'wechart',$channel);
        }

        $channels = ['baidu','360','yingyongbao','xiaomi','oppo','vivo','huawei','offical','sanxing','yingyongbaocpd','wanoujia','qingming2019','register2019','jrtt1','jrtt2','jrtt3','jrtt4','jrtt5'];
        foreach($channels as $channel) {
            self::doAndroidPlatformEveryDay($date,$channel);
        }

        $jrtt_channels = ['01','02','03','04','05','06','07','08','09','010'];
        foreach($jrtt_channels as $channel) {
            self::doIosAndWechartPlatformEveryDay($date,'jrtt',$channel);
        }
    }

    protected static function buildOrderQuery($date,$platform,$channel)
    {
        $start_time = $date . ' 00:00:00';
        $end_time   = $date . ' 23:59:59';
        $query = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time);
        if($platform){
            $query = $query->where('app_platform',$platform);
        }
        if($channel){
            $query = $query->where('app_channel',$channel);
        }
        return $query;
    }

    public static function doIosAndWechartPlatformEveryDay($date,$platform,$channel)
    {
        $start_time = $date . ' 00:00:00';
        $end_time   = $date . ' 23:59:59';
        $user_num = User::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->count();

        $user_ids = User::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->pluck('id')->toArray();

        if($user_ids) {
            $user_order_num = RechargeOrder::query()
                ->where('created_at', '>=', $start_time)
                ->where('created_at', '<=', $end_time)
                ->whereIn('user_id', $user_ids)
                ->select(DB::raw('count(distinct user_id) as total'))
                ->value('total')?:0;
        }else{
            $user_order_num = 0;
        }

        /*
        $user_pay_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->select(DB::raw('count(distinct user_id) as total'))
            ->value('total')?:0;
        */
        $user_pay_num = User::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('order_num','>',0)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->count();

        $order_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->count();
        $nopay_order_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status',0)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->count();
        $pay_order_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->count();

        $recharge_amount = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->select(DB::raw('sum(case type when 1 then (recharge_amount*months) else recharge_amount end) as total'))
            ->value('total')?:0;

        $orderlist = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->get();

        $user_ids = User::query()->where('order_num','>',1)->pluck('id')->toArray();
        $first_order_num = 0;
        $retry_order_num = 0;

        foreach($orderlist as $row){
            if(in_array($row->user_id,$user_ids)){
                $retry_order_num++;
            }else{
                $first_order_num++;
            }
        }

        $mobile_recharge_amount = MobileRechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->select(DB::raw('sum(case type when 1 then (recharge_amount*months) else recharge_amount end) as total'))
            ->value('total')?:0;

        $pay_amount = Pay::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('is_paid',1)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->sum('amount');

        $meal_order_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('type',1)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->count();
        $meal_recharge_amount = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('type',1)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->select(DB::raw('sum(case type when 1 then (recharge_amount*months) else recharge_amount end) as total'))
            ->value('total')?:0;

        $direct_order_num = $pay_order_num - $meal_order_num;
        $direct_recharge_amount = $recharge_amount - $meal_recharge_amount;
        $today_recharge = RechargeTask::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('pay_at','>=',$start_time)
            ->where('pay_at','<=',$end_time)
            ->with('order')
            ->get()->toArray();

        $history_recharge = RechargeTask::query()
            ->where('created_at','<',$start_time)
            ->where('pay_at','>=',$start_time)
            ->where('pay_at','<=',$end_time)
            ->with('order')
            ->get()->toArray();
        $today_recharge_amount = self::resolveAmount($today_recharge,$platform,$channel);
        $history_recharge_amount = self::resolveAmount($history_recharge,$platform,$channel);

        $coupon_ids = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('coupon_id','>',0)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->pluck('coupon_id')->toArray();

        $coupon_amount = 0;
        if($coupon_ids){
            $coupon_amount = UserCoupon::query()->whereIn('id',$coupon_ids)->sum('amount');
        }

        $data = [
            'today'=>$date,'user_num'=>$user_num,'user_pay_num'=>$user_pay_num,'order_num'=>$order_num,'nopay_order_num'=>$nopay_order_num,'pay_order_num'=>$pay_order_num,
            'recharge_amount'=>$recharge_amount,'mobile_recharge_amount'=>$mobile_recharge_amount,'pay_amount'=>$pay_amount,'first_order_num'=>$first_order_num,
            'retry_order_num'=>$retry_order_num,
            'app_platform'=>$platform,'app_channel'=>$channel,'meal_order_num'=>$meal_order_num,'meal_recharge_amount'=>$meal_recharge_amount,
            'direct_order_num'=>$direct_order_num,'direct_recharge_amount'=>$direct_recharge_amount,
            'today_recharge_amount'=>$today_recharge_amount,'history_recharge_amount'=>$history_recharge_amount,'coupon_amount'=>$coupon_amount,'user_order_num'=>$user_order_num
        ];

        $exists = EverydayReport::query()->where('today','=',$date)->where('app_platform',$platform)->where('app_channel',$channel)->first();
        if($exists){
            EverydayReport::query()->where('today',$date)->where('app_platform',$platform)->where('app_channel',$channel)->update($data);
        }else{
            EverydayReport::create($data);
        }
    }

    public static function doAndroidPlatformEveryDay($date,$channel)
    {
        $platform = 'android';
        $start_time = $date . ' 00:00:00';
        $end_time   = $date . ' 23:59:59';
        $user_num = User::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->count();

        $user_ids = User::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->pluck('id')->toArray();

        if($user_ids) {
            $user_order_num = RechargeOrder::query()
                ->where('created_at', '>=', $start_time)
                ->where('created_at', '<=', $end_time)
                ->whereIn('user_id', $user_ids)
                ->select(DB::raw('count(distinct user_id) as total'))
                ->value('total')?:0;
        }else{
            $user_order_num = 0;
        }

        /*
        $user_pay_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->select(DB::raw('count(distinct user_id) as total'))
            ->value('total')?:0;
        */
        $user_pay_num = User::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('order_num','>',0)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->count();

        $order_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->count();
        $nopay_order_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status',0)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->count();
        $pay_order_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->count();

        $recharge_amount = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->select(DB::raw('sum(case type when 1 then (recharge_amount*months) else recharge_amount end) as total'))
            ->value('total')?:0;

        $orderlist = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->get();

        $user_ids = User::query()->where('order_num','>',1)->pluck('id')->toArray();
        $first_order_num = 0;
        $retry_order_num = 0;

        foreach($orderlist as $row){
            if(in_array($row->user_id,$user_ids)){
                $retry_order_num++;
            }else{
                $first_order_num++;
            }
        }

        $mobile_recharge_amount = MobileRechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->select(DB::raw('sum(case type when 1 then (recharge_amount*months) else recharge_amount end) as total'))
            ->value('total')?:0;

        $pay_amount = Pay::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('is_paid',1)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->sum('amount');

        $meal_order_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('type',1)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->count();
        $meal_recharge_amount = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('type',1)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->select(DB::raw('sum(case type when 1 then (recharge_amount*months) else recharge_amount end) as total'))
            ->value('total')?:0;

        $direct_order_num = $pay_order_num - $meal_order_num;
        $direct_recharge_amount = $recharge_amount - $meal_recharge_amount;
        $today_recharge = RechargeTask::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('pay_at','>=',$start_time)
            ->where('pay_at','<=',$end_time)
            ->with('order')
            ->get()->toArray();

        $history_recharge = RechargeTask::query()
            ->where('created_at','<',$start_time)
            ->where('pay_at','>=',$start_time)
            ->where('pay_at','<=',$end_time)
            ->with('order')
            ->get()->toArray();
        $today_recharge_amount = self::resolveAmount($today_recharge,$platform,$channel);
        $history_recharge_amount = self::resolveAmount($history_recharge,$platform,$channel);

        $coupon_ids = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('coupon_id','>',0)
            ->where('app_platform',$platform)
            ->where('app_channel',$channel)
            ->pluck('coupon_id')->toArray();

        $coupon_amount = 0;
        if($coupon_ids){
            $coupon_amount = UserCoupon::query()->whereIn('id',$coupon_ids)->sum('amount');
        }

        $data = [
            'today'=>$date,'user_num'=>$user_num,'user_pay_num'=>$user_pay_num,'order_num'=>$order_num,'nopay_order_num'=>$nopay_order_num,'pay_order_num'=>$pay_order_num,
            'recharge_amount'=>$recharge_amount,'mobile_recharge_amount'=>$mobile_recharge_amount,'pay_amount'=>$pay_amount,'first_order_num'=>$first_order_num,
            'retry_order_num'=>$retry_order_num,
            'app_platform'=>$platform,'app_channel'=>$channel,'meal_order_num'=>$meal_order_num,'meal_recharge_amount'=>$meal_recharge_amount,
            'direct_order_num'=>$direct_order_num,'direct_recharge_amount'=>$direct_recharge_amount,
            'today_recharge_amount'=>$today_recharge_amount,'history_recharge_amount'=>$history_recharge_amount,'coupon_amount'=>$coupon_amount,'user_order_num'=>$user_order_num
        ];

        $exists = EverydayReport::query()->where('today','=',$date)->where('app_platform',$platform)->where('app_channel',$channel)->first();
        if($exists){
            EverydayReport::query()->where('today',$date)->where('app_platform',$platform)->where('app_channel',$channel)->update($data);
        }else{
            EverydayReport::create($data);
        }
    }

    public static function doPlatformEveryDay($date,$platform,$channel)
    {
        $start_time = $date . ' 00:00:00';
        $end_time   = $date . ' 23:59:59';
        $user_num = User::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('app_platform',$platform)
            ->count();

        $user_ids = User::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('app_platform',$platform)
            ->pluck('id')->toArray();

        if($user_ids) {
            $user_order_num = RechargeOrder::query()
                ->where('created_at', '>=', $start_time)
                ->where('created_at', '<=', $end_time)
                ->whereIn('user_id', $user_ids)
                ->select(DB::raw('count(distinct user_id) as total'))
                ->value('total')?:0;
        }else{
            $user_order_num = 0;
        }

        /*
        $user_pay_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('app_platform',$platform)
            ->select(DB::raw('count(distinct user_id) as total'))
            ->value('total')?:0;
        */
        $user_pay_num = User::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('order_num','>',0)
            ->where('app_platform',$platform)
            ->count();

        $order_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('app_platform',$platform)
            ->count();
        $nopay_order_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status',0)
            ->where('app_platform',$platform)
            ->count();
        $pay_order_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('app_platform',$platform)
            ->count();

        $recharge_amount = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('app_platform',$platform)
            ->select(DB::raw('sum(case type when 1 then (recharge_amount*months) else recharge_amount end) as total'))
            ->value('total')?:0;



        $mobile_recharge_amount = MobileRechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('app_platform',$platform)
            ->select(DB::raw('sum(case type when 1 then (recharge_amount*months) else recharge_amount end) as total'))
            ->value('total')?:0;

        $orderlist = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('app_platform',$platform)
            ->get();

        $user_ids = User::query()->where('order_num','>',1)->pluck('id')->toArray();
        $first_order_num = 0;
        $retry_order_num = 0;

        foreach($orderlist as $row){
            if(in_array($row->user_id,$user_ids)){
                $retry_order_num++;
            }else{
                $first_order_num++;
            }
        }

        $pay_amount = Pay::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('is_paid',1)
            ->where('app_platform',$platform)
            ->sum('amount');

        $meal_order_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('type',1)
            ->where('app_platform',$platform)
            ->count();
        $meal_recharge_amount = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('type',1)
            ->where('app_platform',$platform)
            ->select(DB::raw('sum(case type when 1 then (recharge_amount*months) else recharge_amount end) as total'))
            ->value('total')?:0;

        $direct_order_num = $pay_order_num - $meal_order_num;
        $direct_recharge_amount = $recharge_amount - $meal_recharge_amount;
        $today_recharge = RechargeTask::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('pay_at','>=',$start_time)
            ->where('pay_at','<=',$end_time)
            ->with('order')
            ->get()->toArray();

        $history_recharge = RechargeTask::query()
            ->where('created_at','<',$start_time)
            ->where('pay_at','>=',$start_time)
            ->where('pay_at','<=',$end_time)
            ->with('order')
            ->get()->toArray();
        $today_recharge_amount = self::resolveAmount($today_recharge,$platform);
        $history_recharge_amount = self::resolveAmount($history_recharge,$platform);

        $coupon_ids = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('coupon_id','>',0)
            ->where('app_platform',$platform)
            ->pluck('coupon_id')->toArray();

        $coupon_amount = 0;
        if($coupon_ids){
            $coupon_amount = UserCoupon::query()->whereIn('id',$coupon_ids)->sum('amount');
        }

        $data = [
            'today'=>$date,'user_num'=>$user_num,'user_pay_num'=>$user_pay_num,'order_num'=>$order_num,'nopay_order_num'=>$nopay_order_num,'pay_order_num'=>$pay_order_num,
            'recharge_amount'=>$recharge_amount,'mobile_recharge_amount'=>$mobile_recharge_amount,'pay_amount'=>$pay_amount,'first_order_num'=>$first_order_num,
            'retry_order_num'=>$retry_order_num,
            'app_platform'=>$platform,'app_channel'=>$channel,'meal_order_num'=>$meal_order_num,'meal_recharge_amount'=>$meal_recharge_amount,
            'direct_order_num'=>$direct_order_num,'direct_recharge_amount'=>$direct_recharge_amount,
            'today_recharge_amount'=>$today_recharge_amount,'history_recharge_amount'=>$history_recharge_amount,'coupon_amount'=>$coupon_amount,'user_order_num'=>$user_order_num
        ];

        $exists = EverydayReport::query()->where('today','=',$date)->where('app_platform',$platform)->where('app_channel',$channel)->first();
        if($exists){
            EverydayReport::query()->where('today',$date)->where('app_platform',$platform)->where('app_channel',$channel)->update($data);
        }else{
            EverydayReport::create($data);
        }
    }

    public static function doEveryDay($date)
    {
        $start_time = $date . ' 00:00:00';
        $end_time   = $date . ' 23:59:59';
        $user_num = User::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->count();

        $user_ids = User::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->pluck('id')->toArray();

        if($user_ids) {
            $user_order_num = RechargeOrder::query()
                ->where('created_at', '>=', $start_time)
                ->where('created_at', '<=', $end_time)
                ->whereIn('user_id', $user_ids)
                ->select(DB::raw('count(distinct user_id) as total'))
                ->value('total')?:0;
        }else{
            $user_order_num = 0;
        }
        /*
        $user_pay_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->select(DB::raw('count(distinct user_id) as total'))
            ->value('total')?:0;
        */


        $user_pay_num = User::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('order_num','>',0)
            ->count();

        $order_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->count();
        $nopay_order_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status',0)
            ->count();
        $pay_order_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->count();

        $orderlist = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->get();

        $user_ids = User::query()->where('order_num','>',1)->pluck('id')->toArray();
        $first_order_num = 0;
        $retry_order_num = 0;

        foreach($orderlist as $row){
            if(in_array($row->user_id,$user_ids)){
                $retry_order_num++;
            }else{
                $first_order_num++;
            }
        }

        $recharge_amount = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->select(DB::raw('sum(case type when 1 then (recharge_amount*months) else recharge_amount end) as total'))
            ->value('total')?:0;

        $mobile_recharge_amount = MobileRechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->select(DB::raw('sum(case type when 1 then (recharge_amount*months) else recharge_amount end) as total'))
            ->value('total')?:0;

        $pay_amount = Pay::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('is_paid',1)
            ->sum('amount');

        $meal_order_num = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('type',1)
            ->count();
        $meal_recharge_amount = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('type',1)
            ->select(DB::raw('sum(case type when 1 then (recharge_amount*months) else recharge_amount end) as total'))
            ->value('total')?:0;

        $direct_order_num = $pay_order_num - $meal_order_num;
        $direct_recharge_amount = $recharge_amount - $meal_recharge_amount;
        $today_recharge = RechargeTask::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('pay_at','>=',$start_time)
            ->where('pay_at','<=',$end_time)
            ->with('order')
            ->get()->toArray();

        $history_recharge = RechargeTask::query()
            ->where('created_at','<',$start_time)
            ->where('pay_at','>=',$start_time)
            ->where('pay_at','<=',$end_time)
            ->with('order')
            ->get()->toArray();
        $today_recharge_amount = self::resolveAmount($today_recharge);
        $history_recharge_amount = self::resolveAmount($history_recharge);

        $coupon_ids = RechargeOrder::query()
            ->where('created_at','>=',$start_time)
            ->where('created_at','<=',$end_time)
            ->where('status','>',0)
            ->where('coupon_id','>',0)
            ->pluck('coupon_id')->toArray();

        $coupon_amount = 0;
        if($coupon_ids){
            $coupon_amount = UserCoupon::query()->whereIn('id',$coupon_ids)->sum('amount');
        }



        $platform='all';
        $channel='all';
        $data = [
            'today'=>$date,'user_num'=>$user_num,'user_pay_num'=>$user_pay_num,'order_num'=>$order_num,'nopay_order_num'=>$nopay_order_num,'pay_order_num'=>$pay_order_num,
            'recharge_amount'=>$recharge_amount,'mobile_recharge_amount'=>$mobile_recharge_amount,'pay_amount'=>$pay_amount,'first_order_num'=>$first_order_num,
            'retry_order_num'=>$retry_order_num,'app_platform'=>$platform,'app_channel'=>$channel,'meal_order_num'=>$meal_order_num,'meal_recharge_amount'=>$meal_recharge_amount,
            'direct_order_num'=>$direct_order_num,'direct_recharge_amount'=>$direct_recharge_amount,
            'today_recharge_amount'=>$today_recharge_amount,'history_recharge_amount'=>$history_recharge_amount,'coupon_amount'=>$coupon_amount,'user_order_num'=>$user_order_num
        ];

        $exists = EverydayReport::query()->where('today','=',$date)->where('app_platform','all')->first();
        if($exists){
            EverydayReport::query()->where('today',$date)->where('app_platform','all')->update($data);
        }else{
            EverydayReport::create($data);
        }
    }

    protected static function resolveAmount($datalist,$platform=null,$channel=null)
    {
        $amount = 0;
        if($datalist){
            foreach($datalist as $row){
                if($platform && $platform != $row['order']['app_platform']) continue;
                if($channel && $channel != $row['order']['app_channel']) continue;
                $amount += $row['amount'];
            }
        }
        return $amount;
    }
}